﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SYH.Utils;
namespace admin.syh876.com.Code
{
    public class IdentityHelper
    {
        public static AdminUser AdminUser
        {
            get
            {
                AdminUser adminUser = null;
                if (HttpContext.Current.Session["CurrentAdmin"] != null)
                {
                    adminUser = HttpContext.Current.Session["CurrentAdmin"] as AdminUser;
                }
                else
                {
                    LoginAdmin.GetLoginByCookie(ref adminUser);
                    if (adminUser.LoginResult)
                    {
                        HttpContext.Current.Session["CurrentAdmin"] = adminUser;
                    }
                }
                if (adminUser.LoginResult)
                {
                    return adminUser;
                }
                else
                {
                    JsHelper.ParentRedirect("/Admin/");
                    return adminUser;
                }
            }
            set
            {
                HttpContext.Current.Session["CurrentAdmin"] = value;
            }
        }

        public static bool IsAllow(string controller, string action)
        {
            UserAuthorizeAttribute objUserAuthorize = new UserAuthorizeAttribute();
            return objUserAuthorize.IsAllowed(AdminUser, controller, action);
        }

        public static void LoginOut()
        {
            AdminUser = null;
            HttpCookie objCookie = new HttpCookie("adminInfo");

            TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
            objCookie.Expires = DateTime.Now.Add(ts);
            HttpContext.Current.Response.AppendCookie(objCookie);
            JsHelper.ParentRedirect("/Admin/");
        }
    }
}